﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBService;
using DBService.ViewDatas;

public partial class EditUserProfile : System.Web.UI.Page
{
    int UserID = 0;
    UserInfo user = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (int.TryParse((string)Request.QueryString["id"], out UserID))
        {
            InitPage();
        }
        else
        {
            Response.Redirect("~/");
        }
        if (UserID != int.Parse(Session["UserID"].ToString()))
        {
            Response.Redirect("~/");
        }
    }
    public void InitPage()
    {
        ModelAdapter adapter = new ModelAdapter();
        UserInfo userInfo = adapter.GetUser(UserID);
        user = userInfo;
        Name.Value = userInfo.FirstName;
        LastName.Value = userInfo.LastName;
        //FemaleRad.Checked = userInfo.gender == true ? false : true;
        //MaleRad.Checked = userInfo.gender == true ? true : false;
        //MonthSelect.DataValueField = userInfo.bithDate.Month.ToString();
        //DaySelect.DataValueField = userInfo.bithDate.Day.ToString();
        //YearSelect.DataValueField = userInfo.bithDate.Year.ToString();
        Email.Value = userInfo.mail;
        ContentPlaceHolder1_LogoImg.Src = userInfo.Avatar == null ? "images/no-avatar.jpeg" : "upload/" + userInfo.Avatar;

    }


    protected void OnSubmit(object sender, EventArgs e)
    {
        ModelAdapter ma = new ModelAdapter();
        string img = "";
        if (AvatarIMG.HasFile && new string[] { ".gif", ".png", ".jpeg", ".jpg" }.Contains(System.IO.Path.GetExtension(AvatarIMG.FileName).ToLower()))
        {
            img = Guid.NewGuid().ToString().Substring(0, 10) + System.IO.Path.GetFileName(AvatarIMG.FileName);
            AvatarIMG.SaveAs(Server.MapPath("upload") + "\\" + img);
        }
        var _oldpassword = oldPassword == null ? null : oldPassword.Value.ToString();
        var _newpassword = NewPassword == null ? null : NewPassword.Value.ToString();
        var Isvalid = true;
        if (!string.IsNullOrEmpty(_oldpassword) && string.IsNullOrEmpty(_newpassword))
        {
            ErrorID.Text = "შეიყვანეთ ახალი პაროლი";
            Isvalid = false;
        }
        if (string.IsNullOrEmpty(_oldpassword) && !string.IsNullOrEmpty(_newpassword))
        {
            ErrorID.Text = "შეიყანეთ ძველი პაროლი";
            Isvalid = false;
        }
        if (!string.IsNullOrEmpty(_oldpassword) && !string.IsNullOrEmpty(_newpassword) && string.IsNullOrEmpty(_oldpassword) != string.IsNullOrEmpty(_newpassword))
        {
            ErrorID.Text = "შეიყვენეთ ახალი პაროლი,ძველისგანდ განსხვავებული";
            Isvalid = false;
        }
        if (!string.IsNullOrEmpty(_oldpassword) && user.Password == _oldpassword)
        {
            ErrorID.Text = "ძველი პაროლი არასწორია";
            Isvalid = false;
        }

        if (Isvalid)
        {
            ma.EdiUserProfile(UserID
                              , img
                              , Name.Value
                              , LastName.Value
                              , Email.Value
                              , _oldpassword
                              , _newpassword
                              , 1);
        }
    }
}